﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;

using KaoQin.Common;
using AForge.Video.DirectShow;
using KaoQin.Model;
using KaoQin.BLL;


namespace KaoQin.WPFApp.sys
{
    /// <summary>
    /// Setings.xaml 的交互逻辑
    /// </summary>
    public partial class Setings : Window
    {
        public Setings()
        {
            InitializeComponent();
        }

        private FilterInfoCollection videoDevices;
        private VideoCaptureDevice videoDevice;

        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            this.initCam();
            this.initData();
        }

        private void initData()
        {
            SysParameters parasbll = new SysParameters();
            SysWebcamParas webcambll = new SysWebcamParas();
            SysWorkTimeParas workbll = new SysWorkTimeParas();
            try
            {
                SysParametersInfo parasModel = parasbll.GetModel(1);
                SysParametersInfo parasModel1 = parasbll.GetModel(2);

                if (parasModel != null)
                {
                    switch (parasModel.Value1)
                    {
                        case "0":
                            rbICCard.IsChecked = true;
                            break;
                        case "1":
                            rbUserName.IsChecked = true;
                            break;
                        case "2":
                            rbUserNameAndICCard.IsChecked = true;
                            break;
                    }

                    
                    
                }

                if (parasModel1 != null)
                {
                    switch (parasModel1.Value1)
                    {
                        case "0":
                            cbFullScreen.IsChecked = false;
                            break;
                        case "1":
                            cbFullScreen.IsChecked = true;
                            break;
                        
                    }



                }

                SysWebcamParasInfo webcamModel = webcambll.GetModel("48165275-EE69-4E01-A232-7BD4A9E28D4F");

                if (webcamModel!=null)
                {
                    webcamComboBox.SelectedValue = webcamModel.value;
                    cbUsed.IsChecked = webcamModel.KState == 1 ? true : false;
                }

                SysWorkTimeParasInfo workModel = workbll.GetModel("435B4B55-817D-433E-BD50-1CEAB38DFDC8");

                if (workModel != null)
                {
                    BeginStart1.Value = workModel.BeginStart1;
                    BeginStop1.Value = workModel.BeginStop1;
                    BeginTime1.Value =workModel.BeginTime1;
                    EndTime1.Value = workModel.EndTime1;
                    EndStart1.Value = workModel.EndStart1;
                    EndStop1.Value = workModel.EndStop1;

                    BeginStart2.Value = workModel.BeginStart2;
                    BeginStop2.Value = workModel.BeginStop2;
                    BeginTime2.Value = workModel.BeginTime2;
                    EndTime2.Value = workModel.EndTime2;
                    EndStart2.Value = workModel.EndStart2;
                    EndStop2.Value = workModel.EndStop2;

                    BeginStart3.Value = workModel.BeginStart3;
                    BeginStop3.Value = workModel.BeginStop3;
                    BeginTime3.Value = workModel.BeginTime3;
                    EndTime3.Value = workModel.EndTime3;
                    EndStart3.Value = workModel.EndStart3;
                    EndStop3.Value = workModel.EndStop3;

                    BeginDelayTime.Value = workModel.BeginDelayTime;
                    EndForwardTime.Value = workModel.EndForwardTime;

                    cbprohibit.IsChecked = workModel.ProhibitNoTime.Value == 0 ? false : true;
                }

                btnStop.IsEnabled = false;
            }
            catch (Exception ex)
            {
                Logger.Write("初始化数据失败:" + ex.Message);
            }
        }

       
        /// <summary>
        /// 初始化摄像头数据
        /// </summary>
        private void initCam()
        {
            try
            {
                videoDevices = new FilterInfoCollection(FilterCategory.VideoInputDevice);
                List<webcamModel> list = new List<webcamModel>();
                if (videoDevices.Count != 0)
                {
                    
                    foreach (AForge.Video.DirectShow.FilterInfo device in videoDevices)
                    {
                        list.Add(new webcamModel() { Name = device.Name, MonikerString = device.MonikerString });

                    }
                    

                }
                else
                {
                    list.Add(new webcamModel() { Name = "没有找到视频设备", MonikerString = "没有找到视频设备" });
                    txtTip.Text = "没有找到视频设备";
                }

                webcamComboBox.ItemsSource = list;
            }
            catch (Exception ex)
            {
                Logger.Write("初始化出错：" + ex.Message);
                txtTip.Text = "初始化出错";
            }
        }

        private void webcamComboBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            try
            {
                if (videoDevices.Count != 0)
                {
                    videoDevice = new VideoCaptureDevice(videoDevices[webcamComboBox.SelectedIndex].MonikerString);
                    
                }

                
            }
            catch (Exception ex)
            {
                Logger.Write("选择设备失败：" + ex.Message);
                txtTip.Text = "选择设备失败";
            }
            
        }

        

        private void btnSave_Click(object sender, RoutedEventArgs e)
        {
            SysWorkTimeParas workbll = new SysWorkTimeParas();
            SysParameters parasbll = new SysParameters();
            SysWebcamParas webcambll = new SysWebcamParas();
            try
            {
                SysWorkTimeParasInfo workModel = new SysWorkTimeParasInfo();
                workModel.ID = "435B4B55-817D-433E-BD50-1CEAB38DFDC8";

                workModel.BeginStart1 = BeginStart1.Value;
                workModel.BeginStop1 = BeginStop1.Value;
                workModel.BeginTime1 = BeginTime1.Value;
                workModel.EndTime1 = EndTime1.Value;
                workModel.EndStart1 = EndStart1.Value;
                workModel.EndStop1 = EndStop1.Value;

                workModel.BeginStart2 = BeginStart2.Value;
                workModel.BeginStop2 = BeginStop2.Value;
                workModel.BeginTime2 = BeginTime2.Value;
                workModel.EndTime2 = EndTime2.Value;
                workModel.EndStart2 = EndStart2.Value;
                workModel.EndStop2 = EndStop2.Value;

                workModel.BeginStart3 = BeginStart3.Value;
                workModel.BeginStop3 = BeginStop3.Value;
                workModel.BeginTime3 = BeginTime3.Value;
                workModel.EndTime3 = EndTime3.Value;
                workModel.EndStart3 = EndStart3.Value;
                workModel.EndStop3 = EndStop3.Value;

                
                
                workModel.BeginDelayTime = BeginDelayTime.Value;
                workModel.EndForwardTime = EndForwardTime.Value;
                
                workModel.ProhibitNoTime = cbprohibit.IsChecked.Value ? 1 : 0;
                workModel.CreateTime = DateTime.Now;
                
                SysParametersInfo parasModel = new SysParametersInfo();
                parasModel.ID = 1;
                parasModel.KName = "打卡方式";
                parasModel.Value1 = getValue1();
                parasModel.CreateTime = DateTime.Now;

                SysParametersInfo parasModel2 = new SysParametersInfo();
                parasModel2.ID = 2;
                parasModel2.KName = "打卡程序全屏显示";
                parasModel2.Value1 = cbFullScreen.IsChecked.Value ? "1" : "0";
                parasModel2.CreateTime = DateTime.Now;

                SysWebcamParasInfo webcamModel = new SysWebcamParasInfo();
                webcamModel.ID = "48165275-EE69-4E01-A232-7BD4A9E28D4F";
                webcamModel.KName = webcamComboBox.Text;
                webcamModel.value = webcamComboBox.SelectedValue.ToString();
                webcamModel.KState = cbUsed.IsChecked.Value ? 1 : 0;


                workbll.Update(workModel);
                parasbll.Update(parasModel);
                parasbll.Update(parasModel2);
                webcambll.Update(webcamModel);
                

                this.Close();
            }
            catch (Exception ex)
            {
                MessageBox.Show("保存失败:"+ex.Message);
            }
        }

        private string getValue1()
        {
            if (rbICCard.IsChecked.Value)
            {
                return "0";
            }

            if (rbUserName.IsChecked.Value)
            {
                return "1";
            }

            if (rbUserNameAndICCard.IsChecked.Value)
            {
                return "2";
            }

            return "";
        }

        private void Disconnect()
        {
            if (webcamPlayer.VideoSource != null)
            {
                // stop video device
                webcamPlayer.SignalToStop();
                webcamPlayer.WaitForStop();
                webcamPlayer.VideoSource = null;  
            }
        }

        private void btnCancel_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                this.Disconnect();
                this.Close();
            }
            catch (Exception ex)
            {
                Logger.Write("取消时出错：" + ex.Message);
                MessageBox.Show("取消时出错：" + ex.Message);
            }
        }

        private void Window_Closed(object sender, EventArgs e)
        {
            try
            {
                this.Disconnect();
                
            }
            catch (Exception ex)
            {
                Logger.Write("关闭窗口时出错：" + ex.Message);
                MessageBox.Show("关闭窗口时出错：" + ex.Message);
            }
        }

        /// <summary>
        /// 启动摄像头
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnStart_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                if (videoDevice != null)
                {
                    webcamPlayer.VideoSource = videoDevice;
                    webcamPlayer.Start();
                    txtTip.Text = "设备启动成功";
                    btnStop.IsEnabled = true;
                    btnStart.IsEnabled = false;
                }
                
            }
            catch (Exception ex)
            {
                Logger.Write("设备启动失败：" + ex.Message);
                txtTip.Text = "设备启动失败";
            }
            
        }

        /// <summary>
        /// 停止摄像头
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnStop_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                this.Disconnect();
                txtTip.Text = "设备停止成功";
                btnStart.IsEnabled = true;
                btnStop.IsEnabled = false;
            }
            catch (Exception ex)
            {
                Logger.Write("设备停止失败" + ex.Message);
                txtTip.Text = "设备停止失败";
            }
        }
    }

    
}
